/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.oxygens.calculator.swing;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Toolkit;
import ru.oxygens.calculator.service.CalculatorService;

/**
 *
 * @author jz
 */
public class CalculatorFrame extends javax.swing.JFrame
{
private CalculatorService calculatorservice = new CalculatorService();
    /**
     * Creates new form CalculatorFrame
     */
    public CalculatorFrame()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        LabelDisplay = new javax.swing.JLabel();
        panelkeypad = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        buttonback = new javax.swing.JButton();
        buttonres = new javax.swing.JButton();
        button7 = new javax.swing.JButton();
        button8 = new javax.swing.JButton();
        button9 = new javax.swing.JButton();
        buttondiv = new javax.swing.JButton();
        button4 = new javax.swing.JButton();
        button5 = new javax.swing.JButton();
        button6 = new javax.swing.JButton();
        buttonmul = new javax.swing.JButton();
        button1 = new javax.swing.JButton();
        button2 = new javax.swing.JButton();
        button3 = new javax.swing.JButton();
        buttonsub = new javax.swing.JButton();
        button0 = new javax.swing.JButton();
        buttondec = new javax.swing.JButton();
        buttoneq = new javax.swing.JButton();
        buttonadd = new javax.swing.JButton();
        panelStrut = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculator");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        LabelDisplay.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LabelDisplay.setToolTipText("");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(LabelDisplay, gridBagConstraints);

        panelkeypad.setLayout(new java.awt.GridLayout(5, 4));
        panelkeypad.add(jLabel1);
        panelkeypad.add(jLabel2);

        buttonback.setText("back");
        buttonback.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttonbackActionPerformed(evt);
            }
        });
        panelkeypad.add(buttonback);

        buttonres.setText("C");
        buttonres.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttonresActionPerformed(evt);
            }
        });
        panelkeypad.add(buttonres);

        button7.setText("7");
        button7.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button7ActionPerformed(evt);
            }
        });
        panelkeypad.add(button7);

        button8.setText("8");
        button8.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button8ActionPerformed(evt);
            }
        });
        panelkeypad.add(button8);

        button9.setText("9");
        button9.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button9ActionPerformed(evt);
            }
        });
        panelkeypad.add(button9);

        buttondiv.setText("/");
        buttondiv.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttondivActionPerformed(evt);
            }
        });
        panelkeypad.add(buttondiv);

        button4.setText("4");
        button4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button4ActionPerformed(evt);
            }
        });
        panelkeypad.add(button4);

        button5.setText("5");
        button5.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button5ActionPerformed(evt);
            }
        });
        panelkeypad.add(button5);

        button6.setText("6");
        button6.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button6ActionPerformed(evt);
            }
        });
        panelkeypad.add(button6);

        buttonmul.setText("*");
        buttonmul.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttonmulActionPerformed(evt);
            }
        });
        panelkeypad.add(buttonmul);

        button1.setText("1");
        button1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button1ActionPerformed(evt);
            }
        });
        panelkeypad.add(button1);

        button2.setText("2");
        button2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button2ActionPerformed(evt);
            }
        });
        panelkeypad.add(button2);

        button3.setText("3");
        button3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button3ActionPerformed(evt);
            }
        });
        panelkeypad.add(button3);

        buttonsub.setText("-");
        buttonsub.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttonsubActionPerformed(evt);
            }
        });
        panelkeypad.add(buttonsub);

        button0.setText("0");
        button0.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                button0ActionPerformed(evt);
            }
        });
        panelkeypad.add(button0);

        buttondec.setText(",");
        buttondec.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttondecActionPerformed(evt);
            }
        });
        panelkeypad.add(buttondec);

        buttoneq.setText("=");
        buttoneq.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttoneqActionPerformed(evt);
            }
        });
        panelkeypad.add(buttoneq);

        buttonadd.setText("+");
        buttonadd.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                buttonaddActionPerformed(evt);
            }
        });
        panelkeypad.add(buttonadd);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        getContentPane().add(panelkeypad, gridBagConstraints);

        panelStrut.setMinimumSize(new java.awt.Dimension(0, 30));

        javax.swing.GroupLayout panelStrutLayout = new javax.swing.GroupLayout(panelStrut);
        panelStrut.setLayout(panelStrutLayout);
        panelStrutLayout.setHorizontalGroup(
            panelStrutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelStrutLayout.setVerticalGroup(
            panelStrutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(panelStrut, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button0ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button0ActionPerformed
    {//GEN-HEADEREND:event_button0ActionPerformed
         calculatorservice.input0();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button0ActionPerformed

    private void button1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button1ActionPerformed
    {//GEN-HEADEREND:event_button1ActionPerformed
         calculatorservice.input1();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button1ActionPerformed

    private void button2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button2ActionPerformed
    {//GEN-HEADEREND:event_button2ActionPerformed
         calculatorservice.input2();
         LabelDisplay.setText(calculatorservice.getDisplayString());        // TODO add your handling code here:
    }//GEN-LAST:event_button2ActionPerformed

    private void button3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button3ActionPerformed
    {//GEN-HEADEREND:event_button3ActionPerformed
         calculatorservice.input3();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button3ActionPerformed

    private void button4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button4ActionPerformed
    {//GEN-HEADEREND:event_button4ActionPerformed
         calculatorservice.input4();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button4ActionPerformed

    private void button5ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button5ActionPerformed
    {//GEN-HEADEREND:event_button5ActionPerformed
         calculatorservice.input5();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button5ActionPerformed

    private void button6ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button6ActionPerformed
    {//GEN-HEADEREND:event_button6ActionPerformed
         calculatorservice.input6();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button6ActionPerformed

    private void button7ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button7ActionPerformed
    {//GEN-HEADEREND:event_button7ActionPerformed
         calculatorservice.input7();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button7ActionPerformed

    private void button8ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button8ActionPerformed
    {//GEN-HEADEREND:event_button8ActionPerformed
         calculatorservice.input8();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button8ActionPerformed

    private void button9ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_button9ActionPerformed
    {//GEN-HEADEREND:event_button9ActionPerformed
         calculatorservice.input9();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_button9ActionPerformed

    private void buttondecActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttondecActionPerformed
    {//GEN-HEADEREND:event_buttondecActionPerformed
         calculatorservice.inputDecimalSeparator();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttondecActionPerformed

    private void buttoneqActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttoneqActionPerformed
    {//GEN-HEADEREND:event_buttoneqActionPerformed
         calculatorservice.equal();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttoneqActionPerformed

    private void buttonaddActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonaddActionPerformed
    {//GEN-HEADEREND:event_buttonaddActionPerformed
         calculatorservice.add();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttonaddActionPerformed

    private void buttonsubActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonsubActionPerformed
    {//GEN-HEADEREND:event_buttonsubActionPerformed
         calculatorservice.subtract();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttonsubActionPerformed

    private void buttonmulActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonmulActionPerformed
    {//GEN-HEADEREND:event_buttonmulActionPerformed
         calculatorservice.multiply();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttonmulActionPerformed

    private void buttondivActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttondivActionPerformed
    {//GEN-HEADEREND:event_buttondivActionPerformed
         calculatorservice.divide();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttondivActionPerformed

    private void buttonresActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonresActionPerformed
    {//GEN-HEADEREND:event_buttonresActionPerformed
         calculatorservice.reset();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttonresActionPerformed

    private void buttonbackActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonbackActionPerformed
    {//GEN-HEADEREND:event_buttonbackActionPerformed
         calculatorservice.backspace();
         LabelDisplay.setText(calculatorservice.getDisplayString());
    }//GEN-LAST:event_buttonbackActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                CalculatorFrame form = new CalculatorFrame();
                placeWindowToScreenCenter(form);
                form.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelDisplay;
    private javax.swing.JButton button0;
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JButton button3;
    private javax.swing.JButton button4;
    private javax.swing.JButton button5;
    private javax.swing.JButton button6;
    private javax.swing.JButton button7;
    private javax.swing.JButton button8;
    private javax.swing.JButton button9;
    private javax.swing.JButton buttonadd;
    private javax.swing.JButton buttonback;
    private javax.swing.JButton buttondec;
    private javax.swing.JButton buttondiv;
    private javax.swing.JButton buttoneq;
    private javax.swing.JButton buttonmul;
    private javax.swing.JButton buttonres;
    private javax.swing.JButton buttonsub;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel panelStrut;
    private javax.swing.JPanel panelkeypad;
    // End of variables declaration//GEN-END:variables


    public static void placeWindowToScreenCenter(
            java.awt.Window _Window)
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        //Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(null);
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
                GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
        Dimension dialogSize = _Window.getSize();
        _Window.setLocation(
                screenInsets.left + (screenSize.width - screenInsets.left - screenInsets.right - dialogSize.width) / 2,
                screenInsets.top + (screenSize.height - screenInsets.top - screenInsets.bottom - dialogSize.height) / 2);
    }
    
}
